package com.demo.lcgl.controller;

import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.demo.lcgl.controller.base.BaseController;
import com.demo.lcgl.entity.RevenueManagement;
import com.demo.lcgl.service.RevenueManagementService;

/**
 * 收入管理：(RevenueManagement)表控制层
 */
@RestController
@RequestMapping("/revenue_management")
public class RevenueManagementController extends BaseController<RevenueManagement, RevenueManagementService> {

    /**
     * 收入管理对象
     */
    @Autowired
    public RevenueManagementController(RevenueManagementService service) {
        setService(service);
    }

    @PostMapping("/add")
    @Transactional
    public Map<String, Object> add(HttpServletRequest request) throws IOException {
        Map<String, Object> paramMap = service.readBody(request.getReader());
        this.addMap(paramMap);
        String sql = "SELECT MAX(revenue_management_id) AS max FROM " + "revenue_management";
        Integer max = service.selectBaseCount(sql);
        sql =
                "UPDATE `family_account` INNER JOIN `revenue_management` ON family_account.account=revenue_management.account SET family_account"
                        + ".balance = family_account.balance + revenue_management.revenue_amount WHERE revenue_management.revenue_management_id="
                        + max;
        service.updateBaseSql(sql);
        return success(1);
    }

}
